package com.keshihao.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.keshihao.eduservice.constant.Course;
import com.keshihao.eduservice.entity.EduCourse;
import com.keshihao.eduservice.entity.EduCourseDescription;
import com.keshihao.eduservice.entity.EduTeacher;
import com.keshihao.eduservice.entity.vo.CourseInfoVo;
import com.keshihao.eduservice.entity.vo.CoursePublishVo;
import com.keshihao.eduservice.entity.vo.CourseQuery;
import com.keshihao.eduservice.mapper.EduCourseDescriptionMapper;
import com.keshihao.eduservice.mapper.EduCourseMapper;
import com.keshihao.eduservice.service.EduChapterService;
import com.keshihao.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.keshihao.eduservice.service.EduVideoService;
import com.keshihao.servicebase.exceptionhandler.TechException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author keshihao
 * @since 2020-05-25
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    EduCourseDescriptionMapper eduCourseDescriptionMapper;

    @Autowired
    EduVideoService eduVideoService;

    @Autowired
    EduChapterService eduChapterService;

    // 添加课程基本信息的方法
    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        // 1.向课程表添加课程基本信息
        EduCourse eduCourse = new EduCourse();

        BeanUtils.copyProperties(courseInfoVo,eduCourse);

        int insert = baseMapper.insert(eduCourse);

        if (insert <= 0){
            // 添加失败
            throw new TechException(20001,"添加课程信息失败");
        }

        // 2.向课程简介表添加课程简介
        EduCourseDescription courseDescription = new EduCourseDescription();

        BeanUtils.copyProperties(courseInfoVo,courseDescription);
        // 设置主键对应
        courseDescription.setId(eduCourse.getId());

        eduCourseDescriptionMapper.insert(courseDescription);

        return eduCourse.getId();

    }

    @Override
    public CourseInfoVo getCourseById(String courseId) {

        CourseInfoVo courseInfoVo = new CourseInfoVo();
        EduCourse eduCourse = baseMapper.selectById(courseId);
        BeanUtils.copyProperties(eduCourse,courseInfoVo);

        EduCourseDescription courseDescription = eduCourseDescriptionMapper.selectById(courseId);
        courseInfoVo.setDescription(courseDescription.getDescription());

        return courseInfoVo;

    }

    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        baseMapper.updateById(eduCourse);
        EduCourseDescription courseDescription = new EduCourseDescription();
        BeanUtils.copyProperties(courseInfoVo,courseDescription);
        eduCourseDescriptionMapper.updateById(courseDescription);
    }

  @Override
  public CoursePublishVo getCoursePublishInfo(String id) {
    CoursePublishVo coursePublishInfo = baseMapper.getCoursePublishInfo(id);
    return coursePublishInfo;
  }

  @Override
  public void pageQuery(Page<EduCourse> pageParam, CourseQuery courseQuery) {
    QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
    queryWrapper.orderByDesc("gmt_create");

    if (courseQuery == null){
      baseMapper.selectPage(pageParam,queryWrapper);
      return;
    }

    String title = courseQuery.getTitle();
    String teacherId = courseQuery.getTeacherId();
    String subjectParentId = courseQuery.getSubjectParentId();
    String subjectId = courseQuery.getSubjectId();

    if (!StringUtils.isEmpty(title)) {
      queryWrapper.like("title", title);
    }

    if (!StringUtils.isEmpty(teacherId) ) {
      queryWrapper.eq("teacher_id", teacherId);
    }

    if (!StringUtils.isEmpty(subjectParentId)) {
      queryWrapper.ge("subject_parent_id", subjectParentId);
    }

    if (!StringUtils.isEmpty(subjectId)) {
      queryWrapper.ge("subject_id", subjectId);
    }

    baseMapper.selectPage(pageParam, queryWrapper);
  }

    @Override
    public void removeCourse(String courseId) {

        // 1.删除小节
        eduVideoService.removeVideoByCourseId(courseId);

        // 2.删除章节
        eduChapterService.removeChapterByCourseId(courseId);

        // 3.删除描述
        eduCourseDescriptionMapper.deleteById(courseId);

        // 4.删除课程本身
        int result = baseMapper.deleteById(courseId);

        if (result == 0) {
            throw new TechException(20001,"删除失败");
        }

    }


}
